//
//  TIMAddGroupViewController.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/7/2.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMAddGroupViewController.h"
#import "TIMAddGroupTabCell.h"
#import "TIMDemoMacro.h"

#import "TIMStorageModel.h"
#import "TIMStorageManager.h"
#import "TIMApi.h"

@interface TIMAddGroupViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *friendData;
@property (nonatomic, strong) NSMutableArray *addGroupArray;

@property (nonatomic, assign) BOOL isSelected;

@end

@implementation TIMAddGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.addGroupArray = [NSMutableArray array];
    self.isSelected = YES;
    self.tabBarController.tabBar.hidden = YES;
    [self setUpNav];
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUpNav
{
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 60, 30);
    leftBtn.titleLabel.font = TPSFontSize(15);
    [leftBtn setTitle:@"返回" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];;
    
    self.navigationItem.title = @"添加群成员";
    self.view.backgroundColor = UIColor.whiteColor;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 65, 25);
    btn.titleLabel.font = TPSFontSize(15);
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(finishClicked) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)finishClicked {
    // 群组内加入自己
    TIMUserInfo *user = [[TIMStorageManager sharedStorageManager] getCurrentUserInfo];
    [self.addGroupArray addObject:user];
    [TIMApi operateGroupWithGroupId:0 operateUserId:nil MsgOperation:TIMOperateGroupTypeCreateGroup groupMembers:self.addGroupArray GroupName:nil completion:^(NSError *error) {
        if (error) {
            NSLog(@"error.code:%ld",error.code);
            [ProgressHUD showHudTipStr:@"创建群组失败"];
        } else {
            NSLog(@"-------------xiadeli-------------创建群组成功ok");
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self.navigationController popViewControllerAnimated:YES];
            }];
            UIAlertController *alert =  [UIAlertController alertControllerWithTitle:@"创建群组成功"
                                                                            message:@"创建群组成功"
                                                                     preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:action];
            [self presentViewController:alert animated:NO completion:nil];
            
            NSDictionary * dic = [TIMStorageManager sharedStorageManager].createGroupIdDic;
            NSString *groupid = [dic valueForKey:@"createGroup"];
            NSLog(@"groupID:%@",groupid);
        }
    }];
}

#pragma mark - Delegate

-  (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.friendData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIde=@"cellIde";
    TIMAddGroupTabCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIde];
    if (cell == nil) {
        cell= [[TIMAddGroupTabCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIde];
    }
    
    TIMUserInfo *user = self.friendData[indexPath.row];
    cell.nameLabel.text = user.showName;
    [cell.headImageView setImage:[UIImage imageNamed:@"tab_mine_unselect"]];
    cell.selectBtn.selected = user.isSelect;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
 
    TIMUserInfo *user = self.friendData[indexPath.row];
    [user setValue:@(!user.isSelect) forKey:@"isSelect"];
    if (user.isSelect) {
        [self.addGroupArray addObject:user];
    } else {
        [self.addGroupArray removeObject:user];
    }
    
    [self.tableView reloadData];
}

#pragma mark - Get

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0.0, 0, Screen_Width, Screen_Height) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setSectionIndexBackgroundColor:[UIColor clearColor]];
        [_tableView setSectionIndexColor:[UIColor darkGrayColor]];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 62, 0, 0)];
        //cell无数据时，不显示间隔线
        UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
        [_tableView setTableFooterView:v];
    }
    return _tableView;
}

- (NSArray *)friendData {
    if (!_friendData) {
        _friendData = [[TIMStorageManager sharedStorageManager] getAllFriendInfo];
    }
    return _friendData;
}

@end
